package 代码随想录.贪心;

public class a45跳跃游戏2 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {2, 3, 0, 1, 4};
        System.out.println(solution.jump(nums));
    }

    static class Solution {
        public int jump(int[] nums) {
            if (nums.length == 1) return 0;
            int cover = 0;
            int cur = 0;
            int ans = 0;
            for (int i = 0; i < nums.length; i++) {
                cover = Math.max(cover, i + nums[i]);
                if (i == cur) {
                    ans++;
                    cur = cover;
                    if (cover >= nums.length - 1) break;
                }
            }
            return ans;
        }
    }

    static class Solution2 {
        public int jump(int[] nums) {
            if (nums.length == 1) return 0;
            int cover = 0;
            int cur = 0;
            int ans = 0;
            for (int i = 0; i < nums.length - 1; i++) {
                cover = Math.max(cover, i + nums[i]);
                if (i == cur) {
                    ans++;
                    cur = cover;
                }
            }
            return ans;
        }
    }
}
